// Main header
#include "../Main.h"

// My header
#include "Controller_ModalDialog.h"

// Fridge
#include "Controller_Main.h"
#include "../Widgets/Button.h"
#include "../Widgets/DialogPanel.h"
#include "../Widgets/StaticText.h"

static const u_int uDIALOG_BUTTON_HEIGHT = 32;
static const u_int uDIALOG_BUTTON_WIDTH = 128;
static const u_int uDIALOG_MARGIN = 24;

DialogPanel* Controller_ModalDialog::s_pxDialogPanel = 0;
bool Controller_ModalDialog::s_bDestroy = false;
void( * Controller_ModalDialog::s_pfnCallback )( const Controller_ModalDialogResult ) = NULL;

void Controller_ModalDialog::Initialise()
{
}

void Controller_ModalDialog::Update()
{
	if( s_bDestroy )
	{
		Destroy();
	}
}

void Controller_ModalDialog::Shutdown()
{
	Controller_Main::RemoveModal( s_pxDialogPanel );
	delete s_pxDialogPanel;
	s_pxDialogPanel = 0;
}

void Controller_ModalDialog::Show( const char* const szMessage, const Controller_ModalDialogType eType )
{
	if( s_pxDialogPanel )
	{
		return;
	}

	s_pxDialogPanel = new DialogPanel( Controller_Main::GetWindowPanel() );
	Controller_Main::AddModal( s_pxDialogPanel );

	u_int uMinSizeX = 0;

	switch( eType )
	{
		case DIALOGTYPE_OKONLY:      ( uMinSizeX = uDIALOG_BUTTON_WIDTH * 1 + uDIALOG_MARGIN * 3 ); break;
		case DIALOGTYPE_OKCANCEL:    ( uMinSizeX = uDIALOG_BUTTON_WIDTH * 2 + uDIALOG_MARGIN * 3 ); break;
		case DIALOGTYPE_YESNO:       ( uMinSizeX = uDIALOG_BUTTON_WIDTH * 2 + uDIALOG_MARGIN * 3 ); break;
		case DIALOGTYPE_YESNOCANCEL: ( uMinSizeX = uDIALOG_BUTTON_WIDTH * 3 + uDIALOG_MARGIN * 3 ); break;
	}

    StaticText* pxMessage = new StaticText( s_pxDialogPanel );
    pxMessage->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
    pxMessage->SetAutoSizes( true );
    pxMessage->SetText( szMessage );

	s_pxDialogPanel->SetSize( MACRO_MAX( uMinSizeX, pxMessage->GetOriginalWidth() + 2 * uDIALOG_MARGIN ), pxMessage->GetOriginalHeight() + uDIALOG_BUTTON_HEIGHT + uDIALOG_MARGIN * 4 );
    s_pxDialogPanel->SetPosition( MIN_SCREEN_WIDTH / 2 - s_pxDialogPanel->GetOriginalWidth() / 2, MIN_SCREEN_HEIGHT / 2 - s_pxDialogPanel->GetOriginalHeight() / 2  );
    s_pxDialogPanel->SetColour( 0xffffffff );
    s_pxDialogPanel->SetHasBorder( true );
	s_pxDialogPanel->SetHasMoveBar( true );

    pxMessage->SetPosition( s_pxDialogPanel->GetOriginalX() + uDIALOG_MARGIN, s_pxDialogPanel->GetOriginalY() + uDIALOG_MARGIN * 2 );
    pxMessage->SetColour( 0x00000000 );

    Button* pxButton = NULL;
    switch( eType )
    {
        case DIALOGTYPE_OKONLY:
        {
            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "OK" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_BUTTON_WIDTH / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( OKCallback, NULL );
        }
        break;

        case DIALOGTYPE_OKCANCEL:
        {
            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "OK" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_BUTTON_WIDTH - uDIALOG_MARGIN / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( OKCallback, NULL );

            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "Cancel" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 + uDIALOG_MARGIN / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( CancelCallback, NULL );
        }
        break;

        case DIALOGTYPE_YESNO:
        {
            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "Yes" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_BUTTON_WIDTH - uDIALOG_MARGIN / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( YesCallback, NULL );

            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "No" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 + uDIALOG_MARGIN / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( NoCallback, NULL );
        }
        break;

        case DIALOGTYPE_YESNOCANCEL:
        {
            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "Yes" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_MARGIN - 3 * uDIALOG_BUTTON_WIDTH / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( YesCallback, NULL );

            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "No" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_BUTTON_WIDTH / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( NoCallback, NULL );

            pxButton = new Button( s_pxDialogPanel );
            pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
            pxButton->SetLabel( "Cancel" );
            pxButton->SetPosition(  s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 + uDIALOG_MARGIN + uDIALOG_BUTTON_WIDTH / 2,
                                    s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
                                    );
            pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
            pxButton->SetButtonPressCallback( CancelCallback, NULL );
        }
        break;
    }
}

void Controller_ModalDialog::OKCallback( void* pUserData )
{
    s_bDestroy = true;

    if( s_pfnCallback )
    {
        s_pfnCallback( DIALOGRESULT_OK );
    }
}

void Controller_ModalDialog::CancelCallback( void* pUserData )
{
    s_bDestroy = true;

    if( s_pfnCallback )
    {
        s_pfnCallback( DIALOGRESULT_CANCEL );
    }
}

void Controller_ModalDialog::YesCallback( void* pUserData )
{
    s_bDestroy = true;

    if( s_pfnCallback )
    {
        s_pfnCallback( DIALOGRESULT_YES );
    }
}

void Controller_ModalDialog::NoCallback( void* pUserData )
{
	s_bDestroy = true;

    if( s_pfnCallback )
    {
        s_pfnCallback( DIALOGRESULT_NO );
    }
}

void Controller_ModalDialog::Destroy()
{
	if( s_pxDialogPanel )
	{
		Controller_Main::RemoveModal( s_pxDialogPanel );
		delete s_pxDialogPanel;
		s_pxDialogPanel = 0;
	}
	s_bDestroy = false;
}

// eof
